

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>29. Restricted Execution &mdash; Python v2.7.8 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7.8',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.7.8 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.7.8 documentation" href="../index.html" />
    <link rel="up" title="The Python Standard Library" href="index.html" />
    <link rel="next" title="29.1. rexec — Restricted execution framework" href="rexec.html" />
    <link rel="prev" title="28.2. codeop — Compile Python code" href="codeop.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="rexec.html" title="29.1. rexec — Restricted execution framework"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="codeop.html" title="28.2. codeop — Compile Python code"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" accesskey="U">The Python Standard Library</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="restricted-execution">
<span id="restricted"></span><h1>29. Restricted Execution<a class="headerlink" href="#restricted-execution" title="Permalink to this headline">¶</a></h1>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">In Python 2.3 these modules have been disabled due to various known and not
readily fixable security holes.  The modules are still documented here to help
in reading old code that uses the <a class="reference internal" href="rexec.html#module-rexec" title="rexec: Basic restricted execution framework. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">rexec</span></tt></a> and <a class="reference internal" href="bastion.html#module-Bastion" title="Bastion: Providing restricted access to objects. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">Bastion</span></tt></a> modules.</p>
</div>
<p><em>Restricted execution</em> is the basic framework in Python that allows for the
segregation of trusted and untrusted code.  The framework is based on the notion
that trusted Python code (a <em>supervisor</em>) can create a &#8220;padded cell&#8217; (or
environment) with limited permissions, and run the untrusted code within this
cell.  The untrusted code cannot break out of its cell, and can only interact
with sensitive system resources through interfaces defined and managed by the
trusted code.  The term &#8220;restricted execution&#8221; is favored over &#8220;safe-Python&#8221;
since true safety is hard to define, and is determined by the way the restricted
environment is created.  Note that the restricted environments can be nested,
with inner cells creating subcells of lesser, but never greater, privilege.</p>
<p>An interesting aspect of Python&#8217;s restricted execution model is that the
interfaces presented to untrusted code usually have the same names as those
presented to trusted code.  Therefore no special interfaces need to be learned
to write code designed to run in a restricted environment.  And because the
exact nature of the padded cell is determined by the supervisor, different
restrictions can be imposed, depending on the application.  For example, it
might be deemed &#8220;safe&#8221; for untrusted code to read any file within a specified
directory, but never to write a file.  In this case, the supervisor may redefine
the built-in <a class="reference internal" href="functions.html#open" title="open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a> function so that it raises an exception whenever the
<em>mode</em> parameter is <tt class="docutils literal"><span class="pre">'w'</span></tt>.  It might also perform a <tt class="xref c c-func docutils literal"><span class="pre">chroot()</span></tt>-like
operation on the <em>filename</em> parameter, such that root is always relative to some
safe &#8220;sandbox&#8221; area of the filesystem.  In this case, the untrusted code would
still see an built-in <a class="reference internal" href="functions.html#open" title="open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a> function in its environment, with the same
calling interface.  The semantics would be identical too, with <a class="reference internal" href="exceptions.html#exceptions.IOError" title="exceptions.IOError"><tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt></a>s
being raised when the supervisor determined that an unallowable parameter is
being used.</p>
<p>The Python run-time determines whether a particular code block is executing in
restricted execution mode based on the identity of the <tt class="docutils literal"><span class="pre">__builtins__</span></tt> object
in its global variables: if this is (the dictionary of) the standard
<a class="reference internal" href="__builtin__.html#module-__builtin__" title="__builtin__: The module that provides the built-in namespace."><tt class="xref py py-mod docutils literal"><span class="pre">__builtin__</span></tt></a> module, the code is deemed to be unrestricted, else it is
deemed to be restricted.</p>
<p>Python code executing in restricted mode faces a number of limitations that are
designed to prevent it from escaping from the padded cell. For instance, the
function object attribute <tt class="xref py py-attr docutils literal"><span class="pre">func_globals</span></tt> and the class and instance object
attribute <tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt> are unavailable.</p>
<p>Two modules provide the framework for setting up restricted execution
environments:</p>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="rexec.html">29.1. <tt class="docutils literal"><span class="pre">rexec</span></tt> &#8212; Restricted execution framework</a><ul>
<li class="toctree-l2"><a class="reference internal" href="rexec.html#rexec-objects">29.1.1. RExec Objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="rexec.html#defining-restricted-environments">29.1.2. Defining restricted environments</a></li>
<li class="toctree-l2"><a class="reference internal" href="rexec.html#an-example">29.1.3. An example</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="bastion.html">29.2. <tt class="docutils literal"><span class="pre">Bastion</span></tt> &#8212; Restricting access to objects</a></li>
</ul>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://grail.sourceforge.net/">Grail Home Page</a></dt>
<dd>Grail, an Internet browser written in Python, uses these modules to support
Python applets.  More information on the use of Python&#8217;s restricted execution
mode in Grail is available on the Web site.</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="codeop.html"
                        title="previous chapter">28.2. <tt class="docutils literal docutils literal docutils literal"><span class="pre">codeop</span></tt> &#8212; Compile Python code</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="rexec.html"
                        title="next chapter">29.1. <tt class="docutils literal docutils literal"><span class="pre">rexec</span></tt> &#8212; Restricted execution framework</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/restricted.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="rexec.html" title="29.1. rexec — Restricted execution framework"
             >next</a> |</li>
        <li class="right" >
          <a href="codeop.html" title="28.2. codeop — Compile Python code"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2014, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jun 29, 2014.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>